package edu.ynu.se.xiecheng.achitectureclass.project.controller;


import edu.ynu.se.xiecheng.achitectureclass.project.po.vo.SoilQualityVO;
import edu.ynu.se.xiecheng.achitectureclass.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/soil")
@Api(tags = "土壤质量监测接口")
public class SoilQualityController  {

    @Autowired
    private SoilPHDataService phService;

    @Autowired
    private SoilNitrogenDataService nitrogenService;

    @Autowired
    private SoilPhosphorusDataService phosphorusService;

    @Autowired
    private SoilPotassiumDataService potassiumService;

    @Autowired
    private SoilTemperatureDataService temperatureService;

    @GetMapping("/current")
    @ApiOperation("获取当前土壤质量数据")
    public ResponseEntity<SoilQualityVO> getCurrentSoilQuality() {
        try {
            SoilQualityVO vo = new SoilQualityVO();

            // 获取最新的PH值数据
            vo.setPh(phService.getLatestPHData().getValue());
            vo.setPhStatus(phService.evaluateStatus(vo.getPh()));

            // 获取最新的氮含量数据
            vo.setNitrogen(nitrogenService.getLatestNitrogenData().getValue());
            vo.setNStatus(nitrogenService.evaluateStatus(vo.getNitrogen()));

            // 获取最新的磷含量数据
            vo.setPhosphorus(phosphorusService.getLatestPhosphorusData().getValue());
            vo.setPStatus(phosphorusService.evaluateStatus(vo.getPhosphorus()));

            // 获取最新的钾含量数据
            vo.setPotassium(potassiumService.getLatestPotassiumData().getValue());
            vo.setKStatus(potassiumService.evaluateStatus(vo.getPotassium()));

            //获取最新的温度数据
            vo.setTemperature(temperatureService.getCurrentSoilTemperature().getValue());

            // 设置固定信息
            vo.setGreenhouseId("E-01");
            vo.setArea(0.68);
            vo.setGrowthStage("开花结果期");

            return ResponseEntity.ok(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError().build();
        }
    }

    // 从传感器获取并存储PH值数据
    @PostMapping("/ph/fetch")
    @ApiOperation("从传感器获取并存储PH值数据")
    public ResponseEntity<String> fetchAndStorePHData() {
        try {
            phService.fetchAndSavePHData();
            return ResponseEntity.ok("PH值数据成功获取并存储！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError()
                    .body("获取并存储PH值数据时发生错误：" + e.getMessage());
        }
    }

    // 从传感器获取并存储氮含量数据
    @PostMapping("/nitrogen/fetch")
    @ApiOperation("从传感器获取并存储氮含量数据")
    public ResponseEntity<String> fetchAndStoreNitrogenData() {
        try {
            nitrogenService.fetchAndSaveNitrogenData();
            return ResponseEntity.ok("氮含量数据成功获取并存储！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError()
                    .body("获取并存储氮含量数据时发生错误：" + e.getMessage());
        }
    }

    // 从传感器获取并存储磷含量数据
    @PostMapping("/phosphorus/fetch")
    @ApiOperation("从传感器获取并存储磷含量数据")
    public ResponseEntity<String> fetchAndStorePhosphorusData() {
        try {
            phosphorusService.fetchAndSavePhosphorusData();
            return ResponseEntity.ok("磷含量数据成功获取并存储！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError()
                    .body("获取并存储磷含量数据时发生错误：" + e.getMessage());
        }
    }

    // 从传感器获取并存储钾含量数据
    @PostMapping("/potassium/fetch")
    @ApiOperation("从传感器获取并存储钾含量数据")
    public ResponseEntity<String> fetchAndStorePotassiumData() {
        try {
            potassiumService.fetchAndSavePotassiumData();
            return ResponseEntity.ok("钾含量数据成功获取并存储！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError()
                    .body("获取并存储钾含量数据时发生错误：" + e.getMessage());
        }
    }


}